<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveTest::Asserts::Difference</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveTest::Asserts::Difference</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/lib/active_test/asserts/difference_rb.html">
                lib/active_test/asserts/difference.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000003">assert_change</a>&nbsp;&nbsp;
      <a href="#M000001">assert_difference</a>&nbsp;&nbsp;
      <a href="#M000004">assert_no_change</a>&nbsp;&nbsp;
      <a href="#M000002">assert_no_difference</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000003" class="method-detail">
        <a name="M000003"></a>

        <div class="method-heading">
          <a href="#M000003" class="method-signature">
          <span class="method-name">assert_change</span><span class="method-args">(object, method, reload = false) {|| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that the output of object.method is the same before and after the
block.
</p>
<pre>
  assert_change @user, :title do
    @user.name = &quot;Miss Bogart&quot;
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000003-source">
<pre>
    <span class="ruby-comment cmt"># File lib/active_test/asserts/difference.rb, line 40</span>
40:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_change</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">reload</span> = <span class="ruby-keyword kw">false</span>)
41:         <span class="ruby-identifier">initial</span> = <span class="ruby-identifier">object</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>)
42:         <span class="ruby-keyword kw">yield</span>
43:         <span class="ruby-identifier">object</span>.<span class="ruby-identifier">reload</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">reload</span>
44:         <span class="ruby-identifier">assert</span> <span class="ruby-identifier">initial</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>), <span class="ruby-node">&quot;#{object}##{method}&quot;</span>
45:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000001" class="method-detail">
        <a name="M000001"></a>

        <div class="method-heading">
          <a href="#M000001" class="method-signature">
          <span class="method-name">assert_difference</span><span class="method-args">(object, method = nil, difference = 1, reload = false) {|| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that there is a numerical difference, of one by default, between
the output of (object).(method) before and after the block is called.
</p>
<p>
Original idea: <a
href="http://project.ioni.st/post/217#post-217">project.ioni.st/post/217#post-217</a>
Reload addition: Ben Curtis
</p>
<pre>
    assert_difference(Publication, :count) do
      post :create, :publication =&gt; {...}
    end
</pre>
<p>
For instances, you can use the reload flag to ensure a fresh record object:
</p>
<pre>
  assert_difference @item, :quantity, 1, true
    @cart.add_product!(@item.product.id)
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000001-source">
<pre>
    <span class="ruby-comment cmt"># File lib/active_test/asserts/difference.rb, line 21</span>
21:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_difference</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">difference</span> = <span class="ruby-value">1</span>, <span class="ruby-identifier">reload</span> = <span class="ruby-keyword kw">false</span>)
22:         <span class="ruby-identifier">initial_value</span> = <span class="ruby-identifier">object</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
23:         <span class="ruby-keyword kw">yield</span>
24:         <span class="ruby-identifier">object</span>.<span class="ruby-identifier">reload</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">reload</span>
25:         <span class="ruby-identifier">assert_equal</span> <span class="ruby-identifier">initial_value</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">difference</span>, <span class="ruby-identifier">object</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>), <span class="ruby-node">&quot;#{object}##{method}&quot;</span>
26:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000004" class="method-detail">
        <a name="M000004"></a>

        <div class="method-heading">
          <a href="#M000004" class="method-signature">
          <span class="method-name">assert_no_change</span><span class="method-args">(obj, method, reload = false) {|| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that the output of object.method is the same before and after the
block.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000004-source">
<pre>
    <span class="ruby-comment cmt"># File lib/active_test/asserts/difference.rb, line 48</span>
48:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_no_change</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">reload</span> = <span class="ruby-keyword kw">false</span>)
49:         <span class="ruby-identifier">initial</span> = <span class="ruby-identifier">object</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>)
50:         <span class="ruby-keyword kw">yield</span>
51:         <span class="ruby-identifier">object</span>.<span class="ruby-identifier">reload</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">reload</span>
52:         <span class="ruby-identifier">assert</span> <span class="ruby-identifier">initial</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>), <span class="ruby-node">&quot;#{object}##{method}&quot;</span>
53:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000002" class="method-detail">
        <a name="M000002"></a>

        <div class="method-heading">
          <a href="#M000002" class="method-signature">
          <span class="method-name">assert_no_difference</span><span class="method-args">(object, method, reload = false, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Asserts that there is no numerical difference between the output of
object.method before and after the block.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000002-source">
<pre>
    <span class="ruby-comment cmt"># File lib/active_test/asserts/difference.rb, line 30</span>
30:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">assert_no_difference</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">reload</span> = <span class="ruby-keyword kw">false</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
31:         <span class="ruby-identifier">assert_difference</span> <span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-value">0</span>, <span class="ruby-identifier">reload</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>
32:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>